import React from 'react';
import {Button} from 'antd';
import {WeaTools, WeaLocaleProvider, WeaDialog, WeaSearchGroup, WeaFormItem} from 'ecCom';
const getLabel = WeaLocaleProvider.getLabel;

import './style/';
import WeaLicenseSubmit from '../wea-license-submit/';
import WeaNonStandard from '../wea-non-standard/';

class WeaLicense extends React.Component {
    state = {visible: false, loading: false, nsusecnt: 0, data: {}};

    constructor(props) {
        super(props);
        this.getButtons = this.getButtons.bind(this);
        this.onCancel = this.onCancel.bind(this);
        this.onShow = this.onShow.bind(this);
    }

    render() {
        const {visible, loading, nsusecnt, data} = this.state;
        const {canEdit, companyName, licenseCode, hrmNum, scCount, expireDate, concurrentFlag, onlineusercount, licensenum, unusedlice} = data;

        const colProps = {labelCol: {span: 8}, wrapperCol: {span: 16}};

        return (
            <WeaDialog
                visible={visible}
                loading={loading}
                title={getLabel(18014, '授权信息')}
                icon="wevicon-wea-license"
                iconBgcolor='#a7adb5'
                style={{width: 630, height: 462}}
                zIndex={100}
                hasScroll={true}
                buttons={this.getButtons()}
                onCancel={this.onCancel}
            >
                <WeaLicenseSubmit ref="WeaLicenseSubmit"/>
                <WeaNonStandard ref="WeaNonStandard"/>
                <WeaSearchGroup title={getLabel(1361, '基本信息')} showGroup={true} center size="large">
                    <WeaFormItem label={getLabel(16898, '授权用户')} {...colProps} underline>
                        <div className="wea-license-item-view"><span>{companyName}</span></div>
                    </WeaFormItem>
                    <WeaFormItem label={getLabel(18639, '标识码')} {...colProps} underline>
                        <div className="wea-license-item-view"><span>{licenseCode}</span></div>
                    </WeaFormItem>
                    <WeaFormItem label={getLabel(15029, '用户数')} {...colProps} underline>
                        <div className="wea-license-item-view"><span>{hrmNum}</span></div>
                    </WeaFormItem>
                    <WeaFormItem label={getLabel(82496, '分部数')} {...colProps} underline>
                        <div className="wea-license-item-view"><span>{scCount}</span></div>
                    </WeaFormItem>
                    <WeaFormItem label={getLabel(741, '结束日期')} {...colProps} underline>
                        <div className="wea-license-item-view"><span>{expireDate}</span></div>
                    </WeaFormItem>
                </WeaSearchGroup>
                <WeaSearchGroup title={getLabel(21519, 'license使用情况')} showGroup={true} center>
                    <WeaFormItem label={getLabel(21522, '已使用数量')} {...colProps} underline>
                        <div className="wea-license-item-view"><span>{concurrentFlag == '1' ? onlineusercount : licensenum}</span></div>
                    </WeaFormItem>
                    <WeaFormItem label={getLabel(21523, '未使用数量')} {...colProps} underline>
                        <div className="wea-license-item-view"><span>{unusedlice}</span></div>
                    </WeaFormItem>
                </WeaSearchGroup>
                <WeaSearchGroup title={getLabel(83304, '非标功能')+getLabel(18095, '启用')+getLabel(622, '情况')} showGroup={true} center>
                    <WeaFormItem label={getLabel(32626, '已启用')+getLabel(1331, '数量')} {...colProps} underline>
                        <div className="wea-license-item-view">
                            {canEdit ? <a href="javascript:;" onClick={() => this.refs['WeaNonStandard'].onShow()}>{nsusecnt}</a> : <span>{nsusecnt}</span>}
                        </div>
                    </WeaFormItem>
                </WeaSearchGroup>
            </WeaDialog>
        );
    }

    getButtons() {
        const {data} = this.state;

        let buttons = [];
        if (data.canEdit) {
            buttons.push(<Button type="primary" onClick={() => this.refs['WeaLicenseSubmit'].onShow()}>{getLabel(18640, '提交license')}</Button>);
        }
        return buttons;
    }

    onCancel() {
        this.setState({visible: false});
    }

    onShow() {
        this.setState({visible: true, loading: true});
        WeaTools.callApi('/api/system/license/licenseInfo', 'POST', {}).then(data => {
            this.setState({loading: false, data: data});
        });

        WeaTools.callApi('/api/portal/unStandardFuncInfo/count', 'POST', {}).then(data => {
            this.setState({nsusecnt: data.onCount});
        });
    }
}

export default WeaLicense;
